﻿using System.Collections.Generic;
using Actors;
using UnityEngine;

namespace GameScene
{
    class ActorContainer
    {
        private Transform Root;
        Dictionary<long, Actor> m_Actors = new Dictionary<long, Actor>();
        
        public void Initialize(string name = "Actors")
        {
            Root = new GameObject(name).transform;
            Object.DontDestroyOnLoad(Root);
        }
        public void AddActor(Actor actor)
        {
            m_Actors.Add(actor.UID, actor);
            if (actor.transform != null)
            {
                actor.transform.SetParent(Root);
                actor.transform.localPosition = Vector3.zero;
            }
        }
        
        public void RemoveActor(Actor actor)
        {
            m_Actors.Remove(actor.UID);
        }
        
        public void RemoveActor(long uid)
        {
            m_Actors.Remove(uid);
        }
        
        public Actor GetActor(long uid)
        {
            return m_Actors.TryGetValue(uid, out var actor) ? actor : null;
        }
        
        public void Update()
        {
            foreach (var actor in m_Actors.Values)
            {
                if (actor.EnableUpdate)
                {
                    actor.Update();
                }
            }
        }

        public void FixedUpdate()
        {
            foreach (var actor in m_Actors.Values)
            {
                if (actor.EnableFixedUpdate)
                {
                    actor.FixedUpdate();
                }
            }
        }
    }
}